package com.sdau.soa.service.impl;

import com.sdau.soa.dao.BjDao;
import com.sdau.soa.service.BjService;
import com.sdau.soa.vo.Bj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BjServiceImpl implements BjService {

    @Autowired
    private BjDao bjDao;

    @Override
    public List<Bj> queryAll() {
        return bjDao.queryAll();
    }

    @Override
    public Bj queryById(int id) {
        return bjDao.queryById(id);
    }

    @Transactional
    @Override
    public boolean insertBj(Bj bj) {
        if (bj.getBjname() != null && "".equals(bj.getBjname())) {
            try {
                int effectedNum = bjDao.insertBj(bj);
                if (effectedNum > 0)
                    return true;
                else {
                    throw new RuntimeException("插入区域信息失败");
                }
            } catch (Exception e) {
                throw new RuntimeException("插入区域信息失败：" + e.getMessage());
            }
        }else
        {
            throw new RuntimeException("区域信息不能为空");
        }
    }

    @Override
    public boolean updateBj(Bj bj) {
        String t = bj.getId()+"";
        if (t.equals("")  && bj.getId()>0) {
            try {
                int effectedNum = bjDao.updateBj(bj);
                if (effectedNum > 0)
                    return true;
                else {
                    throw new RuntimeException("更新区域信息失败");
                }
            } catch (Exception e) {
                throw new RuntimeException("更新区域信息失败：" + e.getMessage());
            }
        }else
        {
            throw new RuntimeException("区域信息不能为空");
        }

    }

    @Override
    public boolean deleteBj(int id)
    {
        if (id > 0) {
            try {
                int effectedNum = bjDao.deleteBj(id);
                if (effectedNum > 0)
                    return true;
                else {
                    throw new RuntimeException("删除区域信息失败");
                }
            } catch (Exception e) {
                throw new RuntimeException("删除区域信息失败：" + e.getMessage());
            }
        }else
        {
            throw new RuntimeException("区域信息不能为空");
        }
    }
}
